package com.unicom.mec.market.controller;


import com.unicom.mec.common.constants.DataDictConstants;
import com.unicom.mec.market.common.ResultBean;
import com.unicom.mec.market.remote.BaseRemote;
import com.unicom.mec.market.service.MarketService;
import com.unicom.mec.market.vo.ApplicationVo;
import com.unicom.mec.market.vo.DictVo;
import com.unicom.mec.market.vo.TemplateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 边缘市场控制层
 *
 * @author 刘亮武
 * @date 2020年06月15日
 */
@Api(value = "/market", tags = "边缘市场管理")
@RestController
@RequestMapping("/market")
public class MarketController {

    @Autowired
    private MarketService marketService;
    @Autowired
    private BaseRemote baseRemote;

    @ApiOperation(value = "查询应用和能力", httpMethod = "GET")
    @GetMapping("/")
    @ResponseBody
    ResultBean<List<ApplicationVo>> findApplications(@ApiParam(value = "行业") @RequestParam(value = "industry", required = false) String industry,
                                                     @ApiParam(value = "类型") @RequestParam(value = "type", required = false) String type,
                                                     @ApiParam(value = "架构") @RequestParam(value = "arch", required = false) String arch,
                                                     @ApiParam(value = "应用类型") @RequestParam(value = "applicationType", required = false) Integer applicationType,
                                                     @ApiParam(value = "应用权限") @RequestParam(value = "applicationPermission", required = false) Integer applicationPermission,
                                                     @ApiParam(value = "租户主键") @RequestParam(value = "tenantId", required = false) Long tenantId) {
        List<ApplicationVo> applicationVos = marketService.findApplications(industry, type, arch, applicationType, applicationPermission, tenantId);
        // 获取行业字典集合
        ResultBean<List<DictVo>> industryList = baseRemote.findDict(DataDictConstants.INDUSTRY_KEY, null);
        Map<String, String> industryMap = industryList.getData().stream().collect(Collectors.toMap(DictVo::getValue, DictVo::getName));
        // 获取类型字典集合
        ResultBean<List<DictVo>> typeList = baseRemote.findDict(DataDictConstants.TYPE_KEY, null);
        Map<String, String> typeMap = typeList.getData().stream().collect(Collectors.toMap(DictVo::getValue, DictVo::getName));
        // 获取架构字典集合
        ResultBean<List<DictVo>> archList = baseRemote.findDict(DataDictConstants.ARCH_KEY, null);
        Map<String, String> archMap = archList.getData().stream().collect(Collectors.toMap(DictVo::getValue, DictVo::getName));
        applicationVos.forEach(applicationVo -> {
            applicationVo.setIndustryTitle(industryMap.get(applicationVo.getIndustry()));
            applicationVo.setTypeTitle(typeMap.get(applicationVo.getType()));
            applicationVo.setArchTitle(archMap.get(applicationVo.getArch()));
        });
        ResultBean<List<ApplicationVo>> result = new ResultBean<>();
        result.setData(applicationVos);
        return result;
    }

    @ApiOperation(value = "查询应用和能力详情", httpMethod = "GET")
    @GetMapping("/{applicationId}")
    @ResponseBody
    ResultBean<ApplicationVo> findApplicationById(@ApiParam(value = "应用主键") @PathVariable("applicationId") Long applicationId) {
        ApplicationVo applicationVo = marketService.findApplicationById(applicationId);
        ResultBean<ApplicationVo> result = new ResultBean<>();
        result.setData(applicationVo);
        return result;
    }

    @ApiOperation(value = "查询应用和能力部署模板", httpMethod = "GET")
    @GetMapping("/{applicationId}/templates")
    @ResponseBody
    ResultBean<List<TemplateVo>> findApplicationTemplates(@ApiParam(value = "应用主键") @PathVariable("applicationId") Long applicationId) {
        List<TemplateVo> templateVos = marketService.findApplicationTemplates(applicationId);
        ResultBean<List<TemplateVo>> result = new ResultBean<>();
        result.setData(templateVos);
        return result;
    }
}
